package com.example.demo99.Service.Impl;

import com.example.demo99.Entity.AfterSaleRequest;
import com.example.demo99.Mapper.AfterSaleMapper;
import com.example.demo99.Service.AfterSaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AfterSaleServiceImpl implements AfterSaleService {

    @Autowired
    private AfterSaleMapper afterSaleMapper;


    public void createRequest(AfterSaleRequest request) {
        // 如果没有 userId，就设置一个默认值或当前登录用户 ID
        //由于只是测试接口，所以妈呀前端的页面登录，所以用户ID写死
        if (request.getUserId() == null) {
            request.setUserId(1L); // ⚠️ 建议从登录信息获取用户 ID
        }


        // 插入请求
        afterSaleMapper.insert(request);
    }


    public void reviewRequest(Long id, String action) {
        String status = "reject".equalsIgnoreCase(action) ? "REJECTED" : "APPROVED";
        afterSaleMapper.updateStatus(id, status);
    }


    public void refund(Long id) {
        AfterSaleRequest request = afterSaleMapper.findById(id);
        if ("APPROVED".equals(request.getStatus())) {
            afterSaleMapper.updateStatus(id, "REFUNDED");
        }
    }


    public List<AfterSaleRequest> findAll() {
        return afterSaleMapper.findAll();
    }
}